import tkinter as tk
from tkinter import simpledialog
from authentication.auth import UserAuthentication

class MainUI:
    def __init__(self):
        self.root = tk.Tk()
        self.root.title("高铁抢票系统")
        self.auth = UserAuthentication()
        self.setup_ui()
        # 初始化登录成功标志标签
        self.login_success_label = tk.Label(self.root, text="", fg="green")
        self.login_success_label.pack(pady=5)
        # 初始化其他功能框架，先隐藏
        self.query_frame = None
        self.booking_frame = None
        self.payment_frame = None
        self.intelligent_frame = None
        self.data_frame = None

    def setup_ui(self):
        # 用户认证部分
        auth_frame = tk.LabelFrame(self.root, text="用户认证")
        auth_frame.pack(pady=10, padx=10, fill=tk.X)

        login_btn = tk.Button(auth_frame, text="手机号/邮箱登录", command=self.login)
        login_btn.pack(pady=5, padx=5, side=tk.LEFT)

        captcha_btn = tk.Button(auth_frame, text="验证码识别", command=self.recognize_captcha)
        captcha_btn.pack(pady=5, padx=5, side=tk.LEFT)

        # 添加证件号后4位输入框
        self.id_last_four_entry = tk.Entry(auth_frame)
        self.id_last_four_entry.pack(pady=5, padx=5, side=tk.LEFT)

        # 添加获取验证码按钮
        get_sms_btn = tk.Button(auth_frame, text="获取验证码", command=self.get_sms_code)
        get_sms_btn.pack(pady=5, padx=5, side=tk.LEFT)

    def login(self):
        username = simpledialog.askstring("登录", "请输入手机号/邮箱:")
        password = simpledialog.askstring("登录", "请输入密码:", show='*')
        id_last_four = self.id_last_four_entry.get()
        if username and password and id_last_four:
            sms_code = simpledialog.askstring("验证", "请输入短信验证码:")
            if sms_code:
                if self.auth.phone_email_login(username, password, id_last_four, sms_code):
                    # 登录成功，显示标志
                    self.login_success_label.config(text="登录成功！")
                    # 显示其他功能框架
                    self.show_other_functions()
                else:
                    # 登录失败，显示错误信息
                    self.login_success_label.config(text="登录失败，请重试", fg="red")

    def get_sms_code(self):
        id_last_four = self.id_last_four_entry.get()
        if id_last_four:
            result = self.auth.send_sms_code(id_last_four)
            if result:
                print("短信验证码发送成功")
            else:
                print("短信验证码发送失败")

    def recognize_captcha(self):
        captcha_path = simpledialog.askstring("验证码识别", "请输入验证码图片路径:")
        if captcha_path:
            self.auth.captcha_recognition(captcha_path)

    def show_other_functions(self):
        if not self.query_frame:
            # 实时查询部分
            self.query_frame = tk.LabelFrame(self.root, text="实时余票监控")
            self.query_frame.pack(pady=10, padx=10, fill=tk.X)

            start_query_btn = tk.Button(self.query_frame, text="开始查询")
            start_query_btn.pack(pady=5, padx=5, side=tk.LEFT)

            filter_btn = tk.Button(self.query_frame, text="车次过滤")
            filter_btn.pack(pady=5, padx=5, side=tk.LEFT)

        if not self.booking_frame:
            # 自动抢票部分
            self.booking_frame = tk.LabelFrame(self.root, text="自动化抢票")
            self.booking_frame.pack(pady=10, padx=10, fill=tk.X)

            start_booking_btn = tk.Button(self.booking_frame, text="开始抢票")
            start_booking_btn.pack(pady=5, padx=5, side=tk.LEFT)

        if not self.payment_frame:
            # 支付系统部分
            self.payment_frame = tk.LabelFrame(self.root, text="支付系统")
            self.payment_frame.pack(pady=10, padx=10, fill=tk.X)

            lock_btn = tk.Button(self.payment_frame, text="订单锁定")
            lock_btn.pack(pady=5, padx=5, side=tk.LEFT)

            pay_btn = tk.Button(self.payment_frame, text="选择支付通道")
            pay_btn.pack(pady=5, padx=5, side=tk.LEFT)

        if not self.intelligent_frame:
            # 智能服务部分
            self.intelligent_frame = tk.LabelFrame(self.root, text="智能服务")
            self.intelligent_frame.pack(pady=10, padx=10, fill=tk.X)

            recommend_btn = tk.Button(self.intelligent_frame, text="智能车次推荐")
            recommend_btn.pack(pady=5, padx=5, side=tk.LEFT)

            route_btn = tk.Button(self.intelligent_frame, text="路径规划")
            route_btn.pack(pady=5, padx=5, side=tk.LEFT)

            wechat_btn = tk.Button(self.intelligent_frame, text="微信通知")
            wechat_btn.pack(pady=5, padx=5, side=tk.LEFT)

            voice_btn = tk.Button(self.intelligent_frame, text="语音播报")
            voice_btn.pack(pady=5, padx=5, side=tk.LEFT)

        if not self.data_frame:
            # 数据管理部分
            self.data_frame = tk.LabelFrame(self.root, text="数据管理")
            self.data_frame.pack(pady=10, padx=10, fill=tk.X)

            local_btn = tk.Button(self.data_frame, text="本地存储")
            local_btn.pack(pady=5, padx=5, side=tk.LEFT)

            cloud_btn = tk.Button(self.data_frame, text="云端同步")
            cloud_btn.pack(pady=5, padx=5, side=tk.LEFT)

    def run(self):
        self.root.mainloop()